package com.ankicoo.common.response;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * @author jppeng
 */
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ResponseBody<T> implements Serializable {

    /**
     * 状态码
     */
    private int status;
    /**
     * 消息体
     */
    private String msg;
    /**
     * 返回数据
     */
    private T data;

    private ResponseBody(int status) {
        this.status = status;
    }

    private ResponseBody(String msg) {
        this.msg = msg;
    }

    private ResponseBody(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    private ResponseBody(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    private ResponseBody(int status, T data) {
        this.status = status;
        this.data = data;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    public int getStatus() {
        return status;
    }

    public T getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public static <T> ResponseBody<T> createBySuccess() {
        return new ResponseBody<>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ResponseBody<T> createBySuccessMessage(String msg) {
        return new ResponseBody<>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> ResponseBody<T> createBySuccess(T data) {
        return new ResponseBody<>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> ResponseBody<T> createBySuccess(String msg, T data) {
        return new ResponseBody<>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResponseBody<T> createByError() {
        return new ResponseBody<>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    public static <T> ResponseBody<T> createByErrorMessage(String errorMessage) {
        return new ResponseBody<>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> ResponseBody<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new ResponseBody<>(errorCode, errorMessage);
    }

    public static <T> ResponseBody<T> createByErrorMessageData(String errorMessage, T data) {
        return new ResponseBody<>(ResponseCode.ERROR.getCode(), errorMessage, data);
    }
}
